package com.lvideo.component.extraplayer.videoview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;

import com.bestv.app.view.VideoViewListener;
import com.bestv.app.view.VideoViewShell;
import com.bestv.app.view.VideorateInfo;
import com.letv.sdk.callbacks.LetvPlayerView;
import com.lvideo.component.extraplayer.Interface.MyAdStatusListener;
import com.lvideo.component.extraplayer.LVideoMediaPlayerControl;
import com.media.ffmpeg.FFMpegPlayer;
import com.novaplayer.info.PlayUrl;
import com.novaplayer.listener.OnSubtitleListener;
import com.novaplayer.listener.OnVideoViewStateChangeListener;
import com.tencent.qqlive.mediaplayer.api.TVK_PlayerVideoInfo;
import com.tencent.qqlive.mediaplayer.api.TVK_UserInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wangtao
 * @version 1.0
 */

public class VideoViewBestvProxy implements LVideoMediaPlayerControl {

    private Context mContext;
    private VideoViewShell videoViewShell;
    private MediaPlayer.OnPreparedListener mOnPreparedListener;
    private MediaPlayer.OnCompletionListener mOnCompletionListener;
    private MediaPlayer.OnErrorListener mOnErrorListener;
    private MediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener;
    private MediaPlayer.OnInfoListener mOnInfoListener;
    private MyAdStatusListener mOnAdStatusListener;
    private int videoWidth, videoHeight;

    public VideoViewBestvProxy(Context context) {
        this.mContext = context;
        videoViewShell = new VideoViewShell(mContext);
        videoViewShell.initActivity((Activity) context);
        initListener();
    }

    private void initListener() {
        videoViewShell.SetAdCountDownCallbackEnable(true);
        videoViewShell.setPlayerEventListner(new VideoViewListener() {
            @Override
            public void onAdBegin() {
                if(mOnInfoListener != null){
                    mOnInfoListener.onInfo(null, MediaPlayer.MEDIA_INFO_BUFFERING_END, 0);
                }
                mOnAdStatusListener.onAdStarted();
            }

            @Override
            public void onAdCountDown(int i) {
            }

            @Override
            public void onAdEnd() {
                mOnAdStatusListener.onAdFinished();
            }

            @Override
            public void onAdCancel() {
            }



            @Override
            public void onPausingAdShow(boolean b) {
            }

            @Override
            public void onPausingAdCloseButtonClick() {
            }

            @Override
            public void onPreAdClick(String s) {
                Uri uri = Uri.parse(s);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                mContext.startActivity(intent);
            }

            @Override
            public void onPauseAdClick(String s) {
                Uri uri = Uri.parse(s);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                mContext.startActivity(intent);
            }

            @Override
            public void onPlayerClick() {
            }

            @Override
            public void onPrepared(long videoDuration, int videoWidth, int videoHeight) {
                VideoViewBestvProxy.this.videoWidth = videoWidth;
                VideoViewBestvProxy.this.videoHeight = videoHeight;
                if (mOnPreparedListener != null) {
                    mOnPreparedListener.onPrepared(null);
                }
            }

            @Override
            public void onPaused() {
                videoViewShell.play2();
            }

            @Override
            public void onBufferingUpdate(int i) {
            }

            @Override
            public void onBufferStart() {
                if(mOnInfoListener != null){
                    mOnInfoListener.onInfo(null, MediaPlayer.MEDIA_INFO_BUFFERING_START, 0);
                }
            }

            @Override
            public void onBufferEnd() {
                if(mOnInfoListener != null){
                    mOnInfoListener.onInfo(null, MediaPlayer.MEDIA_INFO_BUFFERING_END, 0);
                }
            }

            @Override
            public void onSeekComplete() {
                if (mOnSeekCompleteListener != null) {
                    mOnSeekCompleteListener.onSeekComplete(null);
                }
            }

            @Override
            public void onCompletion() {
                mOnCompletionListener.onCompletion(null);
            }

            @Override
            public boolean onError(int what, int extra) {
                if (mOnErrorListener != null) {
                    mOnErrorListener.onError(null, what, extra);
                }
                return false;
            }

            @Override
            public boolean onNetStreamingReport(int i) {
                System.out.println("onNetStreamingReport--->" + i);
                return false;
            }
        });
    }

    @Override
    public void setVideoPlayUrl(PlayUrl playUrl) {

    }

    @Override
    public void setVideoPath(String s) {

    }

    @Override
    public void setVideoPath(String s, Map<String, String> map) {

    }

    @Override
    public void setOnAdListener(MyAdStatusListener myAdStatusListener) {
        this.mOnAdStatusListener = myAdStatusListener;
    }

    @Override
    public void stopPlayback() {
        videoViewShell.stop();
        videoViewShell.release();
    }

    @Override
    public void forward() {

    }

    @Override
    public void rewind() {

    }

    @Override
    public void adjust(int i) {

    }

    @Override
    public View getView() {
        return videoViewShell;
    }

    @Override
    public int getLastSeekWhenDestoryed() {
        return 0;
    }

    @Override
    public boolean isPaused() {
        return videoViewShell.IsPaused();
    }

    @Override
    public boolean isInPlaybackState() {
        return false;
    }

    @Override
    public void setMediaController(MediaController mediaController) {}

    @Override
    public void setOnPreparedListener(MediaPlayer.OnPreparedListener onPreparedListener) {
        this.mOnPreparedListener = onPreparedListener;
    }

    @Override
    public void setOnCompletionListener(MediaPlayer.OnCompletionListener onCompletionListener) {
        this.mOnCompletionListener = onCompletionListener;
    }

    @Override
    public void setOnInfoListener(MediaPlayer.OnInfoListener onInfoListener) {
        this.mOnInfoListener = onInfoListener;
    }

    @Override
    public void setOnErrorListener(MediaPlayer.OnErrorListener onErrorListener) {
        this.mOnErrorListener = onErrorListener;
    }

    @Override
    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener onSeekCompleteListener) {
        this.mOnSeekCompleteListener = onSeekCompleteListener;
    }

    @Override
    public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener onVideoSizeChangedListener) {
    }

    @Override
    public void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener onBufferingUpdateListener) {
    }

    @Override
    public void setVideoViewStateChangeListener(OnVideoViewStateChangeListener onVideoViewStateChangeListener) {
    }

    @Override
    public void setOnAdNumberListener(FFMpegPlayer.OnAdNumberListener OnAdNumberListener) {
    }

    @Override
    public void setOnBlockListener(FFMpegPlayer.OnBlockListener l) {

    }

    @Override
    public void setOnHardDecodeErrorListener(FFMpegPlayer.OnHardDecodeErrorListner l) {

    }

    @Override
    public boolean isEnforcementWait() {
        return false;
    }

    @Override
    public void setEnforcementWait(boolean enforcementWait) {

    }

    @Override
    public boolean isEnforcementPause() {
        return false;
    }

    @Override
    public void setEnforcementPause(boolean enforcementPause) {

    }

    @Override
    public void setCacheSize(int video_size, int audio_size, int picutureSize, int startpic_size) {

    }

    @Override
    public boolean isSohuAdvertInPlayback() {
        return videoViewShell.GetAdState() == VideoViewShell.AD_PreState_Showing;
    }

    @Override
    public void next() {
    }


    @Override
    public void playVideoAt(int index) {

    }

    @Override
    public List<Integer> getSupportDefinitions() {
        List<VideorateInfo> videorateInfoListist = videoViewShell.getAvailableVideorates();
        List<Integer> list = new ArrayList<Integer>();
        if (videorateInfoListist != null && videorateInfoListist.size() > 0) {
            for (VideorateInfo info : videorateInfoListist) {
                if (info != null) {
                    Integer defint = new Integer(info.index+1);
                    list.add(defint);
                }
            }
        }
        return list;
    }

    @Override
    public void changeDefinition(int definition) {
        videoViewShell.SetVideorate(definition-1);
    }

    @Override
    public int getCurrentDefinition() {
        VideorateInfo info = videoViewShell.getCurrentVideorate();
        return info.index+1;
    }

    @Override
    public void setTencentData(TVK_UserInfo userinfo, TVK_PlayerVideoInfo playerinfo, int currPlayTime) {

    }

    @Override
    public void setLetvScreenView(LetvPlayerView letvScreenView) {

    }

    @Override
    public void setLetvData(long vid, int currPlayTime) {

    }

    @Override
    public void setBestvData(String vid, String fdnCode, long currPlayTime) {
        videoViewShell.StartPlay(vid, fdnCode);
        videoViewShell.seekTo(currPlayTime);
    }

    @Override
    public int getFirstErrorCode() {
        return 0;
    }

    @Override
    public MediaPlayer getMediaPlayer() {
        return null;
    }

    @Override
    public void setOnSubtitleListener(OnSubtitleListener onSubtitleListener) {
    }

    @Override
    public void setInitPosition(int i) {

    }

    @Override
    public void switchStreamSource(String s, int i, int i1) {

    }

    @Override
    public int setSourceType(int i) {
        return 0;
    }

    @Override
    public void setParameter(int i, Object o) {

    }

    @Override
    public boolean handleTouchEvent(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void usingSensor(int i, boolean b) {

    }

    @Override
    public void start() {
        videoViewShell.play();
    }

    @Override
    public void pause() {
        videoViewShell.pause();
    }

    @Override
    public int getDuration() {
        return (int)videoViewShell.getDuration();
    }

    @Override
    public int getCurrentPosition() {
        return (int) videoViewShell.getCurrentPosition();
    }

    @Override
    public void seekTo(int pos) {
        videoViewShell.seekTo(pos);
        if (mOnSeekCompleteListener != null) {
            mOnSeekCompleteListener.onSeekComplete(null);
        }
    }

    @Override
    public boolean isPlaying() {
        return videoViewShell.IsPlaying() || videoViewShell.GetAdState() == VideoViewShell.AD_PreState_Showing;
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }

    @Override
    public void setLanguage(String s) {

    }

    @Override
    public String[] getLanguage() {
        return new String[0];
    }

    public void onActivityResume() {
        videoViewShell.onResume();
    }
    public void onActivityPause() {
        videoViewShell.onPause();
    }
    public void onAdPause() {
        videoViewShell.pausePreAd();
    }
    public void onAdStart() {
        videoViewShell.startPreAd();
    }

    @Override
    public void onDestroyData() {
        videoViewShell = null;
    }
}
